package com.physics.mps.common.util;

import cn.hutool.core.util.StrUtil;
import com.physics.mps.common.exception.BusinessException;
import com.physics.mps.entity.enums.CodeEnum;

/**
 * @author mcsoft
 * @since 2023/2/8
 */
public class AssertUtil {


    // ~ 变量常量
    // =================================================================


    // ~ 公有方法
    // =================================================================

    /**
     * 数据为空则报错
     */
    public static void isBlank(String value, String message) throws BusinessException {
        if (StrUtil.isBlank(value)) {
            throw new BusinessException(message);
        }
    }

    public static void isBlank(String value, CodeEnum codeEnum) throws BusinessException {
        if (StrUtil.isBlank(value)) {
            throw new BusinessException(codeEnum);
        }
    }

    /**
     * 对象不为null则报错
     */
    public static void isNotNull(Object o, String message) throws BusinessException {
        if (null != o) {
            throw new BusinessException(message);
        }
    }

    public static void isNotNull(Object o, CodeEnum codeEnum) throws BusinessException {
        if (null != o) {
            throw new BusinessException(codeEnum);
        }
    }

    /**
     * 对象为空则报错
     */
    public static void isNull(Object o, String message) throws BusinessException {
        if (null == o) {
            throw new BusinessException(message);
        }
    }

    public static void isNull(Object o, CodeEnum codeEnum) throws BusinessException {
        if (null == o) {
            throw new BusinessException(codeEnum);
        }
    }

    /**
     * 对象列表中，存在空字符串则报错
     */
    public static void isBlanks(String message, Object... values) throws BusinessException {
        for (Object value : values) {
            if (value instanceof String) {
                if (StrUtil.isBlank((String) value)) {
                    throw new BusinessException(message);
                }
            } else {
                if (null == value) {
                    throw new BusinessException(message);
                }
            }
        }
    }

    public static void isBlanks(CodeEnum codeEnum, Object... values) throws BusinessException {
        for (Object value : values) {
            if (value instanceof String) {
                if (StrUtil.isBlank((String) value)) {
                    throw new BusinessException(codeEnum);
                }
            } else {
                if (null == value) {
                    throw new BusinessException(codeEnum);
                }
            }
        }
    }

    /**
     * 对象列表中，存在对象为空则报错
     */
    public static void isNulls(String message, Object... o) throws BusinessException {
        for (Object o1 : o) {
            if (null == o1) {
                throw new BusinessException(message);
            }
        }
    }

    public static void isNulls(CodeEnum codeEnum, Object... o) throws BusinessException {
        for (Object o1 : o) {
            if (null == o1) {
                throw new BusinessException(codeEnum);
            }
        }
    }

    public static void isNotEqual(CodeEnum codeEnum, Object a, Object b) {
        if (null == a || null == b || !a.equals(b)) {
            throw new BusinessException(codeEnum);
        }
    }

    /**
     * 如果b为False，则报错
     */
    public static void isFalse(CodeEnum codeEnum, boolean b) {
        if (!b) {
            throw new BusinessException(codeEnum);
        }
    }

    // ~ 私有方法
    // =================================================================


    // ~ 依赖注入
    // =================================================================


}
